package com.exm.collectcodenew.activemq.consumer;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class ActiveMQTopicConsumerPersist {
    public static final String ACTIVEMQ_URL ="tcp://59.110.35.177:61616";
    public static final String TOPIC_NAME="topic-persist";

    public static void main(String[] args) throws JMSException {
        //1.创建连接工厂，按照给定的url地址，采用默认用户名和密码
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2.根据连接工厂创建连接
        Connection connection=activeMQConnectionFactory.createConnection();
        connection.setClientID("z3");
        //3.创建session  两个参数，第一个叫事务/第二个叫签收
        Session session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地（队列/topic）
        Topic topic = session.createTopic(TOPIC_NAME);
        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic,"remark...");
        connection.start();
        Message message=topicSubscriber.receive();
        while (null!=message){
            TextMessage textMessage=(TextMessage) message;
            System.out.println("****收到的持久化topic："+textMessage.getText());
            message = topicSubscriber.receive(5000L);
        }
        session.close();
        connection.close();
    }
}
